<?php

class User_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    protected $_name = 'users';
    
    public function LietKeThongTinUser(){
    	//return 'abc';
    	return $this->fetchall()->toArray();
    }
    
    public function getUser($id)
    {
    	$id = (int)$id;
    	$row = $this->fetchRow('id = ' . $id);
    	if (!$row) {
    		echo 'Khong tim thay thong tin User';
    	}
    	return $row->toArray();
    }
    
    
    public function addUser($user_login, $user_pass, $user_name, $user_email, $user_address, $user_phone, $user_registered, $user_status)
    {
    	$data = array(
    			'user_login' => $user_login,
    			'user_pass' => $user_pass,
    			'user_name' => $user_name,
    			'user_email' => $user_email,
    			'user_address' => $user_address,
    			'user_phone' => $user_phone,
    			'user_registered' => $user_registered,
    			'user_status' => $user_status		
    	);
    	$this->insert($data);
    	return $lastID= $this->getAdapter()->lastInsertId();
    	
    }
    public function updateUser($id, $user_login, $user_pass, $user_name, $user_email, $user_address, $user_phone)
    {
    	$data = array(
    			'user_login' => $user_login,
    			'user_pass' => $user_pass,
    			'user_name' => $user_name,
    			'user_email' => $user_email,
    			'user_address' => $user_address,
    			'user_phone' => $user_phone,
    	);
    	$this->update($data, 'id = '. (int)$id);
    }
    public function deleteUser($id)
    {
    	$this->delete('id =' . (int)$id);
    }
    
    public function getUserByUsernameAndPassword($username,$password){
    
    	if($username && $password){
    			
    		$query=$this->select();
    		$query->from('users',array('ID','user_login','user_name'));
    		$query->where('user_pass =?',$password);
    		$query->where('user_login=?',$username);
    			
    		$row= $this->fetchall($query);
    		$count = count($row);
    		if($count==0){
    			//throw new Exception("Could not find row with $username");
    			return $row=0;
    		}
    		else{
    			return $row->toArray();
    		}
    	}
    }	
	
	 public function getemail($user_email){  
    	if($user_email){    			
    		$query=$this->select();
    		$query->from('users',array('ID','user_login','user_name'));
    		$query->where('user_email =?',$user_email);    		
    			
    		$row= $this->fetchall($query);	
			
    		$count = count($row);
    		if($count==0){    			
    			return $row=0;
    		}
    		else{
    			return $row->toArray();
    		}
    	}
    }
	
	 public function updateUserhash($id)
    {
		$timehash = time(); 
		$prand = rand(0123456789,9876543210); 
		$security = md5($prand);
		$checkrshash= md5($timehash.$id).$security;  
    	$data = array(
    			'timehash' => $timehash,
    			'checkrshash' => $checkrshash  		
    	);
		$this->update($data, 'id = '. (int)$id);
		return $checkrshash;   
    }
	
	 public function getUserhash($key)
    {		
		if($key){		
    		$query=$this->select();
    		$query->from('users',array('ID','timehash'));
    		$query->where('checkrshash =?',$key);    		
    			
    		$row= $this->fetchall($query);	
			
    		$count = count($row);
    		if($count==0){    			
    			return $row=0;
    		}
    		else{
    			return $row->toArray();
    		}
    	}
    }
	
	 public function checktimehash($timehash)
    {
		if($timehash){    		
			$timenow = time(); 
			if( $timenow - $timehash < 86400 )
			{
				return 1;
			}
			else
			{
				return 0;
			}		    		
    	}
    }
	
	 public function updateUserPass($id,$password)
    {
    	$data = array(
				'timehash' => "",
    			'checkrshash' => "",
    			'user_pass' =>$password,
    	);
    	$this->update($data, 'id = '. (int)$id);	
    }
    
    public function getnameuser($id)
    {
    	$query=$this->select();
    	$query->from('users',array('user_name'));
    	$query->where('ID =?',$id);
    	 
    	$row= $this->fetchall($query);
    	return $row[0]['user_name'];
    }
	

}

